package ru.rchervot.sanasar.client.place;

import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;
import ru.rchervot.sanasar.client.view.RootPresenter;

import com.google.gwt.event.shared.HandlerManager;


public abstract class AbstractPlace implements Place {

	private final String placeModule;
	private final String placeName;
	protected final HandlerManager eventBus;
	protected final RPCServiceAsync service;
	protected final RootPresenter  rootPresenter;
	
	protected abstract String composePlaceParameters();
	protected abstract void attachPlaceToRoot();
	public abstract void parseParameters(String parameters);
	
	protected AbstractPlace(HandlerManager eventBus, RPCServiceAsync service, 
			String placeModule, String placeName, RootPresenter  rootPresenter) {
		this.eventBus = eventBus;
		this.service = service;
		this.placeModule = placeModule;
		this.placeName = placeName;
		this.rootPresenter = rootPresenter;
	}
	
	@Override
	public void go(String parameters) {
		attachPlaceToRoot();
		parseParameters(parameters);
	}
	
	protected abstract String historyTokenHead();
	
	@Override
	public String historyToken() {
		String parameters = composePlaceParameters();
		
		return historyTokenHead() + 
		(parameters != null
				? parameters + Constants.DELIMITER
				: ""
		);
	}
}
